/**
 * Copyright (C), 2024, xxxx有限公司
 * FileName: ChatController
 * Author:   TLVM
 * Date:     4/17/2024 10:17 PM
 * Description: 测试连接openai
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.iqcctt.orangelightaiassistant.controller;

import com.iqcctt.orangelightaiassistant.service.ChatBookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 〈一句话功能简述〉<br>
 * 〈测试连接openai〉
 *
 * @author TLVM
 * @create 4/17/2024
 * @since 1.0.0
 */
@RestController
public class ChatController {

    private static final Logger log = LoggerFactory.getLogger(ChatController.class);

    @GetMapping("/hello")
    public String hello() {
        return "hello！";
    }

    private final OpenAiChatClient chatClient;

    @Autowired
    public ChatController(OpenAiChatClient chatClient) {
        this.chatClient = chatClient;;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message",defaultValue = "自我介绍并给我讲个笑话。") String message) {
        Prompt prompt = new Prompt(message);
        ChatResponse call = chatClient.call(prompt);
        log.debug("AI回答：{}",call);
        return Map.of("AI的回答:",call);
    }

    @GetMapping("/ai/function_call")
    public String function_call(@RequestParam(value = "message",defaultValue = "自我介绍并给我讲个笑话。") String message) {

        String userPrompt = message;
        Message userMessage = new UserMessage(userPrompt);
        String systemPrompt = "{prompt}";
        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemPrompt);
        Message systemMessage = systemPromptTemplate.createMessage(Map.of("prompt", "你是一个橙光AI助手,你主要功能是:" +
                "1:添加联系人(姓名,手机号,邮箱地址),从用户的聊天信息中截取是否为添加联系人，信息添加成功返回用户的姓名、手机号、邮箱地址给用户，信息添加失败使用尊敬的语气告知用户。" +
                "2:待办任务,从用户的聊天信息中判断是否需要获取的代办任务详细信息参数，例如：明天8点钟通知我开会." +
                "3:发送邮件,从用户的聊天信息中判断是否需要发送邮件." +
                "4:查询天气信息,从用户的聊天信息中判断是否需要查询天气信息." +
                "备注：用户询问其它信息可以进行回答，但是在最后结尾都要加上，我是您的橙光AI助手，请不要偏题哦！"));
        Set<String> functions = new HashSet<>();
        functions.add("currentWeather");
        functions.add("addAddressBook");
        Prompt prompt = new Prompt(List.of(userMessage, systemMessage),
                OpenAiChatOptions.builder().withFunctions(functions).build()
        );
        List<Generation> response = chatClient.call(prompt).getResults();
        log.debug("AI回答：{}",response);
        String result = "";

        for (Generation generation : response){
            String content = generation.getOutput().getContent();
            result += content;
        }

        return result;
    }

    @GetMapping("/ai/streams")
    public Flux<ChatResponse> generateStream(@RequestParam(value = "message",defaultValue = "请自我介绍并给我讲个笑话。") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return chatClient.stream(prompt);
    }
}
